<%--
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
--%>
<%@ include file="/taglibs.jsp" %>
<c:set var="title" value="Timeline: ${actionBean.timeline.name}" scope="request"/>
<s:layout-render name="/layout/normal.jsp">
  <ss:secure roles="admin">
  <s:layout-component name="htmlHead">
    <script type="text/javascript">
      function deleteSegment(id, name) {
        if (confirm('Really delete segment "' + name + '"?')) {
          $('deleteAction').name = "Delete";
          $('delSegmentId').value = id;
          $('deleteForm').submit();
        }
      }

      function editSegment(id, name, startDate, stopDate) {
        $('segmentId').value = id;
        $('name').value = name;
        $('startDate').value = startDate;
        $('stopDate').value = stopDate;
        showEditForm();
      }

      function showEditForm() {
        $('addFormButton').name = 'Edit';
        $('addFormButton').value = 'Save';
        $('addLegend').innerHTML = 'Edit ' + $('name').value;
        Element.show('showAddButton');
      }

      function showAddForm() {
        $('segmentId').value = '';
        $('name').value = '';
        $('startDate').value = '';
        $('stopDate').value = '';
        $('addFormButton').name = 'Add';
        $('addFormButton').value = 'Add';
        $('addLegend').innerHTML = 'Add a Segment';
        Element.hide('showAddButton');
      }
    </script>
  </s:layout-component>
  </ss:secure>
  <s:layout-component name="body">
    <c:set var="segments" value="${xp:sortDesc(actionBean.timeline.segments, 'startDate')}" scope="request"/>
    <dt:table name="segments" class="data" id="segment" pagesize="15">
      <dt:column title="Name">
        <s:link beanclass="org.xptracker.web.TimeSegmentActionBean">
          ${segment.name}
          <s:param name="timelineId" value="${actionBean.timeline.id}"/>
          <s:param name="segmentId" value="${segment.id}"/>
        </s:link>
      </dt:column>
      <dt:column title="Start" property="startDate" format="{0,date} at {0,time}"/>
      <dt:column title="Stop" property="stopDate" format="{0,date} at {0,time}"/>
      <ss:secure roles="admin">
        <dt:column title="" style="vertical-align:top">
          <input type="button" name="" value="Delete" onclick="deleteSegment('${segment.id}', '${xp:jsFriendly(segment.name)}')"/>
          <input type="button" name="" value="Edit"
                 onclick="editSegment('${segment.id}', '${xp:jsFriendly(segment.name)}',
                 '<fmt:formatDate value='${segment.startDate}' pattern='yyyy-MM-dd HH:mm:ss'/>',
                 '<fmt:formatDate value='${segment.stopDate}' pattern='yyyy-MM-dd HH:mm:ss'/>')"/>
        </dt:column>
      </ss:secure>
    </dt:table>

    <ss:secure roles="admin">
      <br/>
      <input id="showAddButton" type="button" value="Add" onclick="showAddForm()" style="display:none"/>

      <s:form beanclass="org.xptracker.web.TimeSegmentsActionBean" style="margin-top:15px">
        <fieldset>
          <legend id="addLegend">Add a Segment</legend>
          <input type="hidden" name="timelineId" value="${actionBean.timeline.id}"/>
          <s:hidden id="segmentId" name="segment.id"/>
          Name:
          <s:text id="name" name="segment.name"/>
          Start:
          <s:text id="startDate" name="segment.startDate" formatType="datetime"/>
          Stop:
          <s:text id="stopDate" name="segment.stopDate" formatType="datetime"/>
          <s:submit id="addFormButton" name="Add" value="Add"/>
        </fieldset>
      </s:form>

      <s:form beanclass="org.xptracker.web.TimeSegmentsActionBean" id="deleteForm" style="display:inline">
        <input type="hidden" name="timelineId" value="${actionBean.timeline.id}"/>
        <input type="hidden" name="segmentId" id="delSegmentId"/>
        <input type="hidden" id="deleteAction" name="" value="1"/>
      </s:form>
    </ss:secure>
    <s:errors>
      <s:errors-header>
        <script type="text/javascript">
          <c:if test="${actionBean.context.eventName eq 'Edit'}">showEditForm();</c:if>
        </script>
      </s:errors-header>
      <span style="display:none"><s:individual-error/></span>
      <s:errors-footer></s:errors-footer>
    </s:errors>
  </s:layout-component>
</s:layout-render>